package com.csii.pos.scalegrade;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csii.adhoc.controller.BaseController;

import com.csii.pos.scalegrade.service.IBusnScaleGrdSuService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.result.APIResult;
import com.csii.result.PageVo;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


 /**
 * @Description: 业务规模档次设置
 * @Author: zhanghao
 * @Date:   2021-07-06
 * @Version: V1.0
 */

@RestController
@RequestMapping("/busnScaleGrdSu")
@Slf4j
public class BusnScaleGrdSuController extends BaseController<BusnScaleGrdSu, IBusnScaleGrdSuService>{
	@Autowired
	private IBusnScaleGrdSuService busnScaleGrdSuService;

	/**
	 * 根据类型(必传)和规模档次查询BUSN_SCALE_GRD_SU表中数据分页列表查询
	 * @Author: zhanghao
	 * @param busnScaleGrdSu 业务规模档次设置实体类
	 * @return
	 */
	//@OperationLog(value = "业务规模档次设置-分页列表查询")
	@PostMapping(value = "/list")
	public APIResult<?> queryPageList(@RequestBody BusnScaleGrdSu busnScaleGrdSu) {
		Page<BusnScaleGrdSu> page = new Page<>(busnScaleGrdSu.getPageNo(), busnScaleGrdSu.getPageSize());
		IPage<BusnScaleGrdSu> pageList = busnScaleGrdSuService.findAllList(page, busnScaleGrdSu);
		return  APIResult.successResult(PageVo.getPageVoFromIPage(pageList));
	}

	 /**
	  * 查询详情时需要查询对应的规模档次名称.
	  * @param busnScaleGrdSu
	  * @return 规模档次名称
	  */
	//@OperationLog(value = "业务规模档次详情-查询规模名称")
	@PostMapping(value = "/getDetail")
	public APIResult<?> findScaleGrdName(@RequestBody BusnScaleGrdSu busnScaleGrdSu){
		String scaleGrdName = busnScaleGrdSuService.findScaleGrdNameById(busnScaleGrdSu);
		return APIResult.successResult(scaleGrdName);
	}

	/**
	 * 业务规模档次信息添加
	 * @Author: zhanghao
	 * @param busnScaleGrdSu
	 * @return
	 */
	//@OperationLog(value = "业务规模档次设置-添加")
	@PostMapping(value = "/add")
	public APIResult<?> addBusnScaleGrdSu(@Validated @RequestBody BusnScaleGrdSu busnScaleGrdSu) {
		//先根据存量规模档次编号、规模类型和规模档次查询数据库是否包含此数据
		boolean isHave = busnScaleGrdSuService.isExistData(busnScaleGrdSu);
		if (isHave) {
			return APIResult.failResult(null,"此规模类型的存量规模档次编号和规模档次在数据库已存在,请重新输入!");
		}
		if (busnScaleGrdSu.getScaleGrdLl() >= busnScaleGrdSu.getScaleGrdUl()) {
			return APIResult.failResult(null,"规模档次下线不能大于等于规模档次上线!");
		}
		busnScaleGrdSuService.addBusnScaleGrdSu(busnScaleGrdSu);
		return APIResult.successResult("添加成功！");
	}

	/**
	 *  根据规模档次id, 规模档次, 规模类型更新BUSN_SCALE_GRD_SU规模档次设置表记录
	 * @Author: zhanghao
	 * @param busnScaleGrdSu
	 * @return
	 */
	//@OperationLog(value = "业务规模档次设置-编辑")
	@PostMapping(value = "/edit")
	public APIResult<?> updateByThreeCondition(@Validated @RequestBody BusnScaleGrdSu busnScaleGrdSu) {
		if (busnScaleGrdSu.getScaleGrdLl() >= busnScaleGrdSu.getScaleGrdUl()) {
			return APIResult.failResult(null,"规模档次下线不能大于等于规模档次上线!");
		}
		busnScaleGrdSuService.updateByThreeCondition(busnScaleGrdSu);
		return APIResult.successResult("编辑成功!");
	}

	 /**
	  * @description:  输入框查询是提示接口
	  * @param: busnScaleGrdSu
	  * @return:  模糊查询list
	  * @author liuzhonghua
	  * @date:
	  */
	 @RequestMapping("queryHint")
	 public APIResult<?> queryHint(@RequestBody BusnScaleGrdSu busnScaleGrdSu) {
		 QueryWrapper<BusnScaleGrdSu> queryWrapper = new QueryWrapper<>();
		 queryWrapper.select("DISTINCT SCALE_GRD").like("SCALE_GRD",busnScaleGrdSu.getScaleGrd());
		 List<BusnScaleGrdSu> list = busnScaleGrdSuService.list(queryWrapper);
		 return APIResult.successResult(list);
	 }

	 /** 根据模档次id, 规模档次, 规模类型 批量生效和失效
	  * @Author: zhanghao
	  * @param busnScaleGrdSusList
	  * @return
	  */
	@PostMapping("/isValidBatch")
	public APIResult<?> isValidBatch(@Validated @RequestBody List<BusnScaleGrdSu> busnScaleGrdSusList) {
		busnScaleGrdSuService.isValidBatch(busnScaleGrdSusList);
		return APIResult.successResult("批量生/失效编辑成功!");
	}


	/**
	 *  根据规模档次id, 规模档次, 规模档次生效日期 批量删除BUSN_SCALE_GRD_SU规模档次设置表记录
	 * @Author: zhanghao
	 * @param busnScaleGrdSu
	 * @return
	 */
	//@OperationLog(value = "业务规模档次设置-删除")
	@PostMapping(value = "/deleteBatch")
	public APIResult<?> deleteBatch(@Validated @RequestBody List<BusnScaleGrdSu> busnScaleGrdSu) {
		busnScaleGrdSuService.removeByThreeCondition(busnScaleGrdSu);
		return APIResult.successResult("批量删除成功!");
	}

//    /**
//    * 导出excel
//    *
//    * @param request
//    * @param busnScaleGrdSuVO
//    */
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletResponse response, BusnScaleGrdSuVO busnScaleGrdSuVO) {
//        BusnScaleGrdSu busnScaleGrdSu  = ConvertUtils.convert(BusnScaleGrdSu.class,busnScaleGrdSuVO);
//        return super.exportXls( busnScaleGrdSu, BusnScaleGrdSu.class, "业务规模档次设置");
//    }

//    /**
//      * 通过excel导入数据
//    *
//    * @param request
//    * @param response
//    * @return
//    */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public APIResult<?> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
//        return APIResult.successResult(super.importExcel(file, BusnScaleGrdSu.class));
//    }
}
